import { Api } from "../../../utils/api.js"
import { Http } from '../../../utils/http.js'
import { TimeToDate } from '../../../utils/timeToDate.js'

const http = new Http()
const timeToDate = new TimeToDate()
const pageSize = 6
var pageNum = 1
var pages = 1

// pages/mall/availableCoupon/availableCoupon.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coupons: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      amount: options.amount,
      unUse: options.userCoupon == 0
    })
    pageNum = 1
    pages = 1
    this.getMyCoupons()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getMyCoupons(true)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMyCoupons(false)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取优惠券列表
   * @param isPullToRefresh 是否是下拉刷新
   *  */
  getMyCoupons: function(isPullToRefresh){
    if(isPullToRefresh){
      pageNum = 1
    }
    var that = this
    if (pageNum <= pages) {
      http.sendHttp(Api.get_my_coupons, {
        data: {
          pageNum: pageNum,
          pageSize: pageSize
        },
        success: function (res) {
          var coupons = res.data.obj.coupons
          for (var index in coupons) {
            coupons[index].tmExpire = timeToDate.formatTime(coupons[index].tmExpire / 1000, 'Y-M-D')
          }
          var mCoupons = []
          if(!isPullToRefresh){
            mCoupons = that.data.coupons
          }
          mCoupons = mCoupons.concat(coupons)
          that.setData({
            coupons: mCoupons,
            pages: res.data.obj.pages
          })
          pageNum++
          pages = res.data.obj.pages
          that.setAvailableCoupons()
        },
        finish: function(){
          setTimeout(() => {
            wx.stopPullDownRefresh()
          }, 1000)
        }
      })
    }
  },

  /**
   * 自定义方法
   */
  setAvailableCoupons: function () {
    var amount = this.data.amount
    var availableCoupons = []
    this.data.coupons.forEach((item, index) => {
      if (item.coupon.meetPrice <= amount) {
        availableCoupons.push(item)
      }
    })
    this.setData({
      availableCoupons: availableCoupons
    })
    if (availableCoupons.length < pageSize && pageNum <= pages){
      this.getMyCoupons(false)
    }
  },

  selectCoupon: function (e) {
    var index = e.currentTarget.dataset.index
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.setAvailableCoupons(this.data.availableCoupons[index])
    wx.navigateBack({ })
  },

  tapUnUse: function (e){
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.setAvailableCoupons(null)
    wx.navigateBack({})
  }
})